package com.example.springai.controller;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.QuestionAnswerAdvisor;
import org.springframework.ai.vectorstore.SearchRequest;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

@RestController
public class ChatClientController {

    @Autowired
    private ChatClient chatClient;

    @Autowired
    private VectorStore vectorStore;

    @GetMapping("/ai")
    String generation(String userInput) {
        return this.chatClient.prompt().user(userInput).call().content();
    }

    @GetMapping(value = "/streamAi", produces = "text/html;charset=UTF-8")
    Flux<String> streamGeneration(String userInput) {
        return chatClient.prompt().user(userInput).stream().content();
    }

    @GetMapping(value = "/ai/ragWithChatClient", produces = "text/html;charset=UTF-8")
    public Flux<String> ragWithChatClient(@RequestParam(value = "message", defaultValue = "synchronized和volatile有什么区别？") String message) {
        return chatClient.prompt()
                .user(message)
                .advisors(new QuestionAnswerAdvisor(vectorStore, SearchRequest.defaults().withTopK(1)))
                .stream()
                .content();
    }

}
